package com.xing.crm.service.impl;

import com.xing.crm.mapper.ProSkuMapper;
import com.xing.crm.pojo.*;

import com.xing.crm.service.ProSkuService;
import com.xing.crm.utils.ParseEditorUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//开启事务？
@Transactional(rollbackFor = Exception.class)

@Service
public class ProSkuServiceImpl implements ProSkuService {
	
	@Resource
	private ProSkuMapper proSkuMapper; //数据库操作对象
	
	/**
	 * 基础数据列表
	 * @param skuPoJo
	 * @param page
	 * @param limit
	 * @return
	 */
	@Override
	public PageResult<ProSkuPoJo> list(ProSkuPoJo skuPoJo, Integer page, Integer limit)
	{
		Map params = new HashMap(0);
		params.put("params", skuPoJo);
		params.put("start", (page - 1) * limit);
		params.put("limit", limit);
		PageResult<ProSkuPoJo> result = new PageResult();
		result.setData(this.proSkuMapper.list(params));
		result.setCount(this.proSkuMapper.count(params).intValue());
		result.setCode(0);
		result.setMsg("查询成功");
		return result;
	}
	
	
	/**
	 * 新增+修改二盒一
	 * @param skuPoJo
	 * @return
	 */
	@Override
	public Map<String, Object> edit(ProSkuPoJo skuPoJo)
	{
		//如果参数中没有ID，则是创建，否则是编辑
		if (skuPoJo.getId() == null) {
			this.proSkuMapper.add(skuPoJo);
		} else {
			//todo 先取出旧的图片删掉
			this.proSkuMapper.edit(skuPoJo);
		}
		Map result = new HashMap(0);
		result.put("code", 0);
		result.put("msg", "操作成功");
		result.put("data", skuPoJo.getId());
		return result;
	}
	
	/**
	 * 详情
	 * @param id
	 * @return
	 */
	@Override
	public ProSkuPoJo detail(Integer id){
		ProSkuPoJo result = this.proSkuMapper.detail(id);
		if(result!=null){
			//由于双精度读取到只显示了1个0，弄成字符串
			result.setGuidePriceStr(result.getGuidePrice().intValue()+"");
			result.setLowestPriceStr(result.getLowestPrice().intValue()+"");
		}
		return result;
		//return this.proSkuMapper.detail(id);
	}
	
	
	/**
	 * 删除记录
	 * @param id
	 * @return
	 */
	@Override
	public Map<String, Object> delete(Integer id)
	{
		/* 封面图*/
		File file;//file操作对象
		List<String> files = new ArrayList<>(id);
		ProSkuPoJo filesAlbum = proSkuMapper.albumFile(id);
		if(filesAlbum != null) {
			files.add(filesAlbum.getCover());
		}
		//目录
		String dir = System.getProperty("user.dir") + File.separator;
		//遍历删除文件
		for(String path: files)
		{
			file = new File(dir, path);
			if(file.exists()) {
				file.delete();
			}
		}
		//删除记录
		if (id != null)
		{
			this.proSkuMapper.delete(id);
		}
		//输出结果
		Map result = new HashMap(0);
		result.put("code", 0);
		result.put("msg", "操作成功");
		return result;
	}
	
	
	//修改排序
	@Override
	public Result sort(ProSkuPoJo proSkuPoJo){
		Result result = new Result();
		int affectedRows = 0;
		if (proSkuPoJo.getId() != null) {
			affectedRows = this.proSkuMapper.sort(proSkuPoJo);
		}
		result.setCode(affectedRows > 0 ? 0 : 1);
		result.setMsg(affectedRows > 0 ? "修改成功" : "没有执行成功");
		return result;
	}
	
}
